package com.slz3.business.module.memberManagement.memJifen.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.memberManagement.memJifen.domain.MemJifen;
import com.slz3.business.module.memberManagement.memJifen.domain.MemJifenFactory;
import com.slz3.business.module.memberManagement.memJifen.steward.MemJifenSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MemJifenServiceImpl implements MemJifenService {

    @Override
    public MemJifen save(MemJifen memJifen) {
        return Repositorys.memJifen.save(MemJifenFactory.initBaseFields(memJifen));
    }

    @Override
    public Page<MemJifen> page(MultiValueMap<String, String> params) {
        Page<MemJifen> memJifenPage = Repositorys.memJifen.findAll(
                MemJifenSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return memJifenPage;
    }

    @Override
    public MemJifen modify(MemJifen memJifenNew) {
        return Repositorys.memJifen.saveAndFlush(MemJifenFactory.initBaseFields(memJifenNew));
    }

    @Override
    public Optional<MemJifen> single(UUID uuid) {
        return Repositorys.memJifen.findById(uuid);
    }

    @Override
    public List<MemJifen> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.memJifen.findAllById(uuids);
    }

    @Override
    public List<MemJifen> delete(List<MemJifen> memJifenList) {
        memJifenList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MemJifenFactory.initBaseFields(item);
        });
        return Services.memJifenTransactional.batchModify(memJifenList);
    }

}
